package lc;

import java.util.LinkedHashMap;

public class LRUCache {

	LinkedHashMap<Integer, Integer> cache = new LinkedHashMap<>(2);
	int capacity;
	
    public LRUCache(int capacity) {
    	this.capacity = capacity;
    }
    
    public int get(int key) {
        if (cache.containsKey(key)) {
        	Integer value = cache.get(key);
        	cache.remove(key);
        	cache.put(key, value);
        	return value;
        }
        else {
        	return -1;
        }
    }
    
    public void set(int key, int value) {
    	if (!cache.containsKey(key) && cache.size() == capacity) {
    		Integer oldKey = cache.keySet().iterator().next();
    		cache.remove(oldKey);
    	}
    	
    	if (cache.containsKey(key)) {
    		cache.remove(key);
    	}
    	cache.put(key, value);
    }
    
    public static void main(String ...strings) {
    	LRUCache sol = new LRUCache(2);
    	int val = sol.get(2);
    	sol.set(2, 1);
    	sol.set(1, 1);
    	sol.set(2, 3);
    	sol.set(4, 1);
    	val = sol.get(1);
    	val = sol.get(2);
    	
    }
    
}
